package com.lineage.server.model.weaponskill;

import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1AttackList;
import com.lineage.server.model.L1Character;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DmgAcMr {
  private static final Log _log = LogFactory.getLog(DmgAcMr.class);
  
  private static final Random _random = new Random();
  
  public static int calcDefense(L1Character target) {
    try {
      if (target instanceof L1PcInstance) {
        L1PcInstance targetPc = (L1PcInstance)target;
        int ac = Math.max(0, 10 - targetPc.getAc());
        int acDefMax = targetPc.getClassFeature().getAcDefenseMax(ac);
        if (acDefMax != 0) {
          int srcacd = Math.max(1, acDefMax >> 3);
          return _random.nextInt(acDefMax) + srcacd;
        } 
      } else if (target instanceof L1MonsterInstance) {
        L1MonsterInstance targetNpc = (L1MonsterInstance)target;
        int damagereduction = targetNpc.getNpcTemplate().get_damagereduction();
        int srcac = targetNpc.getAc();
        int ac2 = Math.max(0, 10 - srcac);
        int acDefMax2 = ac2 / 7;
        if (acDefMax2 != 0) {
          int srcacd2 = Math.max(1, acDefMax2);
          return _random.nextInt(acDefMax2) + srcacd2 + damagereduction;
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return 0;
  }
  
  public static double calcMrDefense(L1PcInstance pc, L1Character target, double dmg, L1ItemInstance weapon) {
    if (weapon.getItem().getType1() == 20)
      return dmg; 
    int TotalMagicHit = 0;
    Integer magichit = (Integer)L1AttackList.INTH.get(Integer.valueOf(pc.getInt()));
    if (magichit != null)
      TotalMagicHit += magichit.intValue(); 
    if (pc.getBaseInt() >= 25 && pc.getBaseInt() <= 44) {
      TotalMagicHit += (pc.getBaseInt() - 15) / 10;
    } else if (pc.getBaseInt() >= 45) {
      TotalMagicHit += 5;
    } 
    TotalMagicHit += pc.getOriginalMagicHit();
    int mr = getTargetMr(target);
    double mrFloor = 0.0D;
    if (mr < 100) {
      mrFloor = Math.floor(((mr - TotalMagicHit) / 2));
    } else if (mr >= 100) {
      mrFloor = Math.floor(((mr - TotalMagicHit) / 10));
    } 
    double mrCoefficient = 0.0D;
    if (mr < 100) {
      mrCoefficient = 1.0D - 0.01D * mrFloor;
    } else if (mr >= 100) {
      mrCoefficient = 0.6D - 0.01D * mrFloor;
    } 
    dmg *= mrCoefficient;
    return dmg;
  }
  
  private static int getTargetMr(L1Character target) {
    int mr = 0;
    if (target instanceof L1PcInstance) {
      L1PcInstance targetPc = (L1PcInstance)target;
      mr = targetPc.getMr();
      switch (targetPc.guardianEncounter()) {
        case 0:
          mr += 3;
          break;
        case 1:
          mr += 6;
          break;
        case 2:
          mr += 9;
          break;
      } 
    } else if (target instanceof L1MonsterInstance) {
      L1MonsterInstance targetNpc = (L1MonsterInstance)target;
      mr = targetNpc.getMr();
    } 
    return mr;
  }
  
  public static double getDamage(L1PcInstance pc) {
    double dmg = 0.0D;
    int spByItem = pc.getSp() - pc.getTrueSp();
    int intel = pc.getInt();
    int charaIntelligence = pc.getInt() + spByItem - 12;
    double coefficientA = 1.0D + 0.09375D * charaIntelligence;
    if (coefficientA < 1.0D)
      coefficientA = 1.0D; 
    double coefficientB = 0.0D;
    if (intel > 25) {
      coefficientB = 1.625D;
    } else if (intel <= 12) {
      coefficientB = 0.78D;
    } else {
      coefficientB = intel * 0.065D;
    } 
    double coefficientC = 0.0D;
    if (intel > 25) {
      coefficientC = 25.0D;
    } else if (intel <= 12) {
      coefficientC = 12.0D;
    } else {
      coefficientC = intel;
    } 
    double bsk = 0.0D;
    if (pc.hasSkillEffect(55))
      bsk = 0.1D; 
    dmg = (_random.nextInt(6) + 1 + 7) * (1.0D + bsk) * coefficientA * coefficientB / 10.5D * coefficientC;
    return dmg;
  }
  
  public static double getSpInt(L1PcInstance pc) {
    int spByItem = pc.getSp() - pc.getTrueSp();
    int intel = pc.getInt();
    return ((spByItem + intel) * 3);
  }
  
  public static double calcMrDefense2(L1PcInstance pc, L1Character target, double dmg, L1ItemInstance weapon) {
    int mr = getTargetMr(target);
    double mrFloor = 0.0D;
    if (mr < 100) {
      mrFloor = Math.floor(((mr - pc.getOriginalMagicHit()) / 2));
    } else if (mr >= 100) {
      mrFloor = Math.floor(((mr - pc.getOriginalMagicHit()) / 10));
    } 
    double mrCoefficient = 0.0D;
    if (mr < 100) {
      mrCoefficient = 1.0D - 0.01D * mrFloor;
    } else if (mr >= 100) {
      mrCoefficient = 0.6D - 0.01D * mrFloor;
    } 
    dmg *= mrCoefficient;
    return dmg;
  }
  
  public static double getDamageStr(L1PcInstance pc) {
    double dmg = 0.0D;
    int intel = pc.getStr();
    int charaIntelligence = pc.getStr();
    double coefficientA = 1.0D + 0.09375D * charaIntelligence;
    if (coefficientA < 1.0D)
      coefficientA = 1.0D; 
    double coefficientB = 0.0D;
    if (intel > 25) {
      coefficientB = 1.625D;
    } else if (intel <= 12) {
      coefficientB = 0.78D;
    } else {
      coefficientB = intel * 0.065D;
    } 
    double coefficientC = 0.0D;
    if (intel > 25) {
      coefficientC = 25.0D;
    } else if (intel <= 12) {
      coefficientC = 12.0D;
    } else {
      coefficientC = intel;
    } 
    double bsk = 0.0D;
    if (pc.hasSkillEffect(55))
      bsk = 0.1D; 
    dmg = (_random.nextInt(6) + 1 + 7) * (1.0D + bsk) * coefficientA * coefficientB / 10.5D * coefficientC;
    return dmg;
  }
  
  public static double getDamageDex(L1PcInstance pc) {
    double dmg = 0.0D;
    int intel = pc.getDex();
    int charaIntelligence = pc.getDex();
    double coefficientA = 1.0D + 0.09375D * charaIntelligence;
    if (coefficientA < 1.0D)
      coefficientA = 1.0D; 
    double coefficientB = 0.0D;
    if (intel > 25) {
      coefficientB = 1.625D;
    } else if (intel <= 12) {
      coefficientB = 0.78D;
    } else {
      coefficientB = intel * 0.065D;
    } 
    double coefficientC = 0.0D;
    if (intel > 25) {
      coefficientC = 25.0D;
    } else if (intel <= 12) {
      coefficientC = 12.0D;
    } else {
      coefficientC = intel;
    } 
    double bsk = 0.0D;
    if (pc.hasSkillEffect(55))
      bsk = 0.1D; 
    dmg = (_random.nextInt(6) + 1 + 7) * (1.0D + bsk) * coefficientA * coefficientB / 10.5D * coefficientC;
    return dmg;
  }
  
  public static double getDamageInt(L1PcInstance pc) {
    double dmg = 0.0D;
    int intel = pc.getInt();
    int charaIntelligence = pc.getInt();
    double coefficientA = 1.0D + 0.09375D * charaIntelligence;
    if (coefficientA < 1.0D)
      coefficientA = 1.0D; 
    double coefficientB = 0.0D;
    if (intel > 25) {
      coefficientB = 1.625D;
    } else if (intel <= 12) {
      coefficientB = 0.78D;
    } else {
      coefficientB = intel * 0.065D;
    } 
    double coefficientC = 0.0D;
    if (intel > 25) {
      coefficientC = 25.0D;
    } else if (intel <= 12) {
      coefficientC = 12.0D;
    } else {
      coefficientC = intel;
    } 
    double bsk = 0.0D;
    if (pc.hasSkillEffect(55))
      bsk = 0.1D; 
    dmg = (_random.nextInt(6) + 1 + 7) * (1.0D + bsk) * coefficientA * coefficientB / 10.5D * coefficientC;
    return dmg;
  }
}
